
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from areas.models import Area
from areas.serializers import AreaSerializer, SubsAreaSerialzier


#收货地址后端接口    GET/areas/infos/(?P<pk>\d+)/
# class AreaModelViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
#     def get_queryset(self):
#         if self.action == 'list':    #查询省
#             return  Area.objects.filter(parent = None)
#         else:
#             return  Area.objects.all()   #查询区县


#收货地址后端接口    GET/areas/infos/(?P<pk>\d+)/
class AreaModelViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    serializer_class = AreaSerializer
    # queryset = Area.objects.filter(parent = None)
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent = None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubsAreaSerialzier